package cn.gdust.dao;

import cn.gdust.entity.Videos;
import cn.gdust.utils.DB;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class VideoDao {
    public List<Videos> findAll() throws SQLException {
        List<Videos> list = new ArrayList<>();
        String sql = "SELECT * FROM videos";
        Connection conn = DB.open();
        Statement stmt = conn.createStatement();
        ResultSet rs = stmt.executeQuery(sql);
        return getVideosAllInfo(list, rs);
    }

    public Videos getVideo(String ID) throws SQLException {
        String sql = "SELECT * FROM videos WHERE id=?";
        Connection conn = DB.open();
        PreparedStatement statement = conn.prepareStatement(sql);
        statement.setString(1, ID);
        ResultSet rs = statement.executeQuery();
        Videos videos = new Videos();
        while (rs.next()) {
            videos.setId(rs.getInt("id"));
            videos.setImg(rs.getString("img"));
            videos.setTitle(rs.getString("title"));
            videos.setDesc(rs.getString("desc"));
            videos.setUrl(rs.getString("url"));
            videos.setVideoLength(rs.getDate("videoLength"));
            videos.setCreatetime(rs.getDate("createtime"));
            videos.setUpdatetime(rs.getDate("updatetime"));
            videos.setTag(rs.getString("tag"));
        }
        return videos;
    }

    /**
     * 查询标签
     * @param tag
     * @return
     * @throws SQLException
     */
    public List<Videos> getTagData(String tag) throws SQLException {
        List<Videos> list = new ArrayList<>();
        String sql = "SELECT * FROM videos WHERE tag=?";
        Connection conn = DB.open();
        PreparedStatement statement = conn.prepareStatement(sql);
        statement.setString(1, tag);
        ResultSet rs = statement.executeQuery();
        return getVideosAllInfo(list, rs);
    }

    /**
     * 搜索
     * @param keyword
     * @return
     * @throws SQLException
     */
    public List<Videos> search(String keyword) throws SQLException {
        List<Videos> list = new ArrayList<>();
        String sql = "SELECT * FROM videos WHERE title LIKE ?";
        Connection conn = DB.open();
        PreparedStatement statement = conn.prepareStatement(sql);
        statement.setString(1, "%" + keyword + "%");
        ResultSet rs = statement.executeQuery();
        return getVideosAllInfo(list, rs);
    }

    /**
     * 获取所有视频信息
     * @param list
     * @param rs
     * @return
     * @throws SQLException
     */
    private List<Videos> getVideosAllInfo(List<Videos> list, ResultSet rs) throws SQLException {
        while (rs.next()) {
            Videos videos = new Videos();
            videos.setId(rs.getInt("id"));
            videos.setImg(rs.getString("img"));
            videos.setTitle(rs.getString("title"));
            videos.setDesc(rs.getString("desc"));
            videos.setUrl(rs.getString("url"));
            videos.setVideoLength(rs.getDate("videoLength"));
            videos.setCreatetime(rs.getDate("createtime"));
            videos.setUpdatetime(rs.getDate("updatetime"));
            videos.setTag(rs.getString("tag"));
            list.add(videos);
        }
        return list;
    }
}
